package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import com.android.kotlincoroutinemvvmdemo.R

/*
* ArrangeView
* */
class ArrangeView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            strokeCap = Paint.Cap.ROUND
            //style = Paint.Style.STROKE
            strokeJoin = Paint.Join.ROUND
            color = context.resources.getColor(R.color.color_14B892)
        }
    }

    private val mPaint1 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.ROUND
            //style = Paint.Style.STROKE
            strokeJoin = Paint.Join.ROUND
            color = context.resources.getColor(R.color.color_259F83)
        }
    }

    private val mPaint2 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.ROUND
            //style = Paint.Style.STROKE
            strokeJoin = Paint.Join.ROUND
            color = context.resources.getColor(R.color.color_62A697)
        }
    }

    private val mPath by lazy {
        Path()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth.toFloat() / 2
            val cy = mHeight.toFloat() / 2
            save()
            translate(cx, cy)

            //mPaint0.strokeWidth = (cx / 18).coerceAtLeast(minStrokeWidth)
            //mPaint1.strokeWidth = (cx / 18).coerceAtLeast(minStrokeWidth)
            //mPaint2.strokeWidth = (cx / 18).coerceAtLeast(minStrokeWidth)

            mPath.apply {
                reset()
                moveTo(-cx * 2 / 5, cy * 1 / 5)
                lineTo(-cx * 4 / 5, cy * 2 / 5)
                lineTo(0F, cy * 4 / 5)
                lineTo(cx * 4 / 5, cy * 2 / 5)
                lineTo(cx * 2 / 5, cy * 1 / 5)

                lineTo(0F, cy * 2 / 5)
                close()
            }
            drawPath(mPath, mPaint0)

            mPath.apply {
                reset()
                moveTo(-cx * 2 / 5, -cy * 1 / 5)
                lineTo(-cx * 4 / 5, 0F)
                lineTo(0F, cy * 2 / 5)
                lineTo(cx * 4 / 5, 0F)
                lineTo(cx * 2 / 5, -cy * 1 / 5)

                lineTo(0F, 0F)
                close()
            }
            drawPath(mPath, mPaint1)

            mPath.apply {
                reset()
                moveTo(0F, -cy * 4 / 5)
                lineTo(-cx * 4 / 5, -cy * 2 / 5)
                lineTo(0F, 0F)
                lineTo(cx * 4 / 5, -cy * 2 / 5)
                close()
            }
            drawPath(mPath, mPaint2)

            restore()
        }
    }
}